<?php

/**
 * 此文件主要用于相关业务逻辑操作
 */
namespace Home\Controller\SaleManager;

use Think\Controller;
use Home\Controller\BaseController;
use Home\Controller\LoginAndRegister\LoginController;
use Home\Model\SaleManager\County\SaleManagerModel;
use Home\Model\LoginAndRegister\CookieModel;
use Home\Common\SalemanagerHelper;
use Home\Model\SaleManager\NoticeModel;
use Home\Model\SaleManager\SalemanApplyModel;
use Home\Model\SaleManager\Clinic\ClinicModel;
use \Think\Model;
use Home\Common\WorksummerHelper;

/**
 * 该文件用于公共操作
 */
class BasicController extends BaseController {
	protected $actions;
	protected $saleModel;
	protected $paramtersMod;
	protected $roleId;
	static $_userinfo;
	protected $lowerRoleId;
	static $user;
	protected $noticeModel;
	protected $saleApplyModel;
	protected  $clinicMod;
	protected  $saleOrderModel;
	protected  $saleTripModel;
	protected  $saleSignModel;
	protected  $systemMassageModel;
	
	/**
	 * 初始化
	 * @param unknown $key
	 */
	function _initialize($key) {
		$this->saleModel = $key;
		parent::_initialize ();
		self::$user = session(C('SESSION_PREFIX').'_salemanager');
		$user = self::$user;
        $user['uid'] = $user['id'];
        //cookie判断
        $where = array('phone'=>cookie('phone'),'password'=>cookie('password'));
        $cookieuser = SalemanagerHelper::getSaleByWhere($where);
        //如果存在cookie，并且访问登录action，则自动登录
        if(!empty($cookieuser[0]) && strtoupper(ACTION_NAME)=='LOGIN'){
        		$sesseionmane = C('SESSION_PREFIX').'_salemanager';
	        	session($sesseionmane,null);
	        	session($sesseionmane,$cookieuser[0]);
	        	if($cookieuser[0]['role_id']==11){
	        		$url = SalemanagerHelper::redirectUrl($cookieuser[0]['role_id'])."indexassistant";
	        	}else{
	        		$url = SalemanagerHelper::redirectUrl($cookieuser[0]['role_id'])."index";
	        	}
	        
	        	$this->redirect($url,'',0,'');exit;
        };
			//session 判断,如果session失效，除了登录，找密码可以访问，其余都跳转到登录界面
			if(strtoupper(ACTION_NAME)!='LOGIN' && strtoupper(ACTION_NAME)!='FINDPASSWORD' && IS_AJAX==false && strtoupper(ACTION_NAME)!='VERIFY'&& strtoupper(ACTION_NAME)!='CODE'){
				if(empty(self::$user)){
					$this->redirect('/Home/SaleManager/Login/login');
				}
			}
			$this->lowerRoleId =SalemanagerHelper::getLowerRoleId($user['id']);
			$user['id'] = think_ucenter_encrypt($user['id']);
			$this->assign('myself',$user);
			$this->noticeModel = new NoticeModel();
			$this->saleApplyModel = new SalemanApplyModel();
			$this->clinicMod = new ClinicModel();
			$this->saleOrderModel = new \Home\Model\SaleManager\SalemanOrderModel();
			$this->saleSignModel = new \Home\Model\SaleManager\SalemanSignModel();
			$this->saleTripModel = new \Home\Model\SaleManager\SalemanTripModel();
			$this->systemMassageModel = new \Home\Model\SaleManager\SystemMessageModel();
			//AJAX提交，判断是否存在session，不存在session，返回相应的code
			if(IS_AJAX){
				if(empty($user)){
					responseJson(2028, C('SALEMANAGER_CODE.2028'),'/Home/SaleManager/Login/login');
				}
			}
	}
	/**
	 * 当前用户对应的下级列表(例如：省总对应下级列表为地总，以此类推)
	 */
	public function index(){
		$this->assign('lists',$this->saleModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'role_id'=>$this->lowerRoleId,'status'=>C('SALEMANAGER_STATUS.NORMAL'))));
		$this->infoList(); //获取消息列表
		//$this->display();
	}
	
	/**
	 * 助理首页
	 */
	public function indexassistant(){
		$this->assign('lists',$this->saleModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'role_id'=>2,'status'=>C('SALEMANAGER_STATUS.NORMAL'))));
		$this->infoList(); //获取消息列表
		//$this->display();
	}
	/**
	 * 获取下级列表
	 */
	public function getLower(){
		$REQUEST = ( array ) I ( 'request.' );
		if(IS_AJAX){ //下拉刷新
			if(!empty($REQUEST['pageStart'])){
				if(self::$user['role_id']==0){
					$userList = $this->saleModel->findInfobyCondition(array('role_id'=>$this->lowerRoleId,'status'=>C('SALEMANAGER_STATUS.NORMAL')),C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart']);
						
				}else{
					if(self::$user['role_id']==1 && strtoupper(ACTION_NAME)=='MYSALEMAN'){
						//获取所有省总[大区经理下面]
						$childrens = WorksummerHelper::getAllChildrenId(self::$user['id']);
						$map=array('role_id'=>$this->lowerRoleId,'status'=>C('SALEMANAGER_STATUS.NORMAL'));
						$map['id']  = array('in',$childrens);
						$userList = $this->saleModel->findInfobyCondition($map,C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart']);	
					}else{
						$userList = $this->saleModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'role_id'=>$this->lowerRoleId,'status'=>C('SALEMANAGER_STATUS.NORMAL')),C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart']);	
					}			
				}
				}else{
				$userList=array();
			}			
		}else{
			//默认页面渲染
			if(self::$user['role_id']==0){
				$userList = $this->saleModel->findInfobyCondition(array('role_id'=>$this->lowerRoleId,'status'=>C('SALEMANAGER_STATUS.NORMAL')),C('SALEMANAGER_PAGE_NUM'),0);
			}else{
				//大区经理
				if(self::$user['role_id']==1  && strtoupper(ACTION_NAME)=='MYSALEMAN'){
					//获取所有省总
					$childrens = WorksummerHelper::getAllChildrenId(self::$user['id']);
					$map=array('role_id'=>$this->lowerRoleId,'status'=>C('SALEMANAGER_STATUS.NORMAL'));
					$map['id']  = array('in',$childrens);
					$userList = $this->saleModel->findInfobyCondition($map,C('SALEMANAGER_PAGE_NUM'),0);
				}else{
					$userList = $this->saleModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'role_id'=>$this->lowerRoleId,'status'=>C('SALEMANAGER_STATUS.NORMAL')),C('SALEMANAGER_PAGE_NUM'),0);
				}
			}
				
		}
		$this->assign('userid',think_ucenter_encrypt($REQUEST['userid']));
		foreach ($userList as $k=>$v){
			$userList[$k]['id']=think_ucenter_encrypt($v['id']);
		}
		return $userList;
	}
	
	/**
	 * 个人信息详情
	 */
	public  function personData(){
		if(IS_POST){
			$re = self::editMyself();
			if($re['status']==1){
				$this->assign('status','success');
				$this->assign('msg',$re['msg']);
				$userid =think_ucenter_decrypt(self::$user['id']);
				$user = $this->saleModel->findInfobyCondition(array("id"=>self::$user['id']));
				$user[0]['id']=$userid;
				$this->assign('user',$user[0]);
				$this->display();
			}else{
				$this->assign('status','error');
				$this->assign('msg',$re['msg']);
				$userid =think_ucenter_decrypt(self::$user['id']);
				$user = $this->saleModel->findInfobyCondition(array("id"=>self::$user['id']));
				$user[0]['id']=$userid;
				$this->assign('user',$user[0]);
				$this->display();
			}
		}else{
			self::detail();
		}
	}

	/**
	 * 我的下级列表(县总下级是业务员，地总下级是县总)
	 */
	public function mySaleMan() {
		$type = I ( 'get.t' );
		if(IS_AJAX){
			//新增我的助手
			if(self::$user['role_id']==1 && I ( 'get.t' )=='assistant'){
				$userList = self::getAssistant();
			}else{
				$userList = self::getLower();
			}
			return responseJson(200, '刷新成功',$userList);
		}else{
			if(self::$user['role_id']==1&& $type=='assistant'){
				$userList = self::getAssistant();
// 				echo M()->getlastsql();exit;
			}else{
				$userList = self::getLower();
			}
			//$userList = self::getLower();
			$this->assign('lists',$userList);
			if(self::$user['role_id']!=C('SALE_MANAGER.SALE_COUNTY')){
				if(empty($userList)){
					if(self::$user['role_id']==1 &&$type=='assistant'){
						$url = SalemanagerHelper::redirectUrl(self::$user['role_id'])."noAssistant";
					}else{
						$url = SalemanagerHelper::redirectUrl(self::$user['role_id'])."noSaleMan";
					}
					
					$this->redirect($url);
					exit;
				}
				
			}
			if(self::$user['role_id']==1 && $type=='assistant'){
				$this->display('myassistant');exit;
			}
			
			$this->display();
		}	
	}
	
	
	/**
	 * 更换业务员（获取业务员列表）
	 */
	public  function changeSale(){
		$REQUEST = ( array ) I ( 'request.' );
		if(IS_POST){
			
			$data['saleman_id'] = think_ucenter_decrypt($REQUEST['select1']);
			$model = new Model();
        		$model->startTrans();
        		$re = self::changeLower($this->clinicMod, array('saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user'])), $data);//更换诊所
        		$applyCondition=array(
        				'saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
        		);
        		$apply2Condition=array(
        				'superior_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
        		);
        		//申请支持更换
        		$appRe = $this->saleApplyModel->updateFieldByCondition($applyCondition, array("saleman_id"=>$data['saleman_id']));
        		
        		$app2Re = $this->saleApplyModel->updateFieldByCondition($apply2Condition, array("superior_id"=>$data['saleman_id']));
        			
        		$noticeCondition=array(
        				'send_user_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
        		);
        		$notice2Condition=array(
        				'accept_saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
        		);
        		//消息更换主人
        		$noticeRe = $this->noticeModel->updateFieldByCondition($noticeCondition, array("send_user_id"=>$data['saleman_id']));
        			
        		$notice2Re = $this->noticeModel->updateFieldByCondition($notice2Condition, array("accept_saleman_id"=>$data['saleman_id']));
        			
        		//订单更换
        		$orderCondition=array(
        				'send_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
        		);
        		$order2Condition=array(
        				'saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
        		);
        		$orderRe = $this->saleOrderModel->updateFieldByCondition($orderCondition, array("send_id"=>$data['saleman_id']));
        			
        		$order2Re = $this->saleOrderModel->updateFieldByCondition($order2Condition, array("saleman_id"=>$data['saleman_id']));
        			
						
						//签到更换
			$signCondition=array(
					'saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			$signRe = $this->saleSignModel->updateFieldByCondition($signCondition, array("saleman_id"=>$data['saleman_id']));	
			//工作计划更换
			$tripCondition=array(
					'saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			$tripRe = $this->saleTripModel->updateFieldByCondition($signCondition, array("saleman_id"=>$data['saleman_id']));
			//被替换的人设置为禁用	
			$saleResult = $this->saleModel->updateField (think_ucenter_decrypt($REQUEST['origin_user']),array('status'=>C('SALEMANAGER_STATUS.FORBIDDEN')) );
			if($re['status']==1&&$appRe['status']==1&& $app2Re['status']==1 &&$saleResult['status']==1&&$noticeRe['status']==1&&$notice2Re['status']==1&&$orderRe['status']==1&&$order2Re['status']==1&&$signRe['status']==1&&$tripRe['status']==1){
				$model->commit();
				//根据角色跳转
				 //如果是大区经理更换助理
				if(self::$user['role_id']==1 && $REQUEST['t']=='assistant'){
					$url = SalemanagerHelper::redirectUrl(self::$user['role_id'])."mySaleMan/t/assistant";
				 }else{
				 	$url = SalemanagerHelper::redirectUrl(self::$user['role_id'])."mySaleMan";
				 }
				
				$this->redirect($url);
				//redirect('../../mySaleMan');
			}else{
				$model->rollback();
				$this->assign('status','error');
				$this->assign('msg','更换出错');
				$this->display();
			}
		}else{
			if(self::$user['role_id']==1 &&$REQUEST['t']=='assistant'){
				$userList = self::getAssistant();
			}else{
				$userList = self::getLower();
			}
			$this->assign('lists',$userList);
			$this->assign('userid',$REQUEST['userid']);
			if(empty($userList) || ($userList[0]['id']==$REQUEST['userid']&&count($userList)==1)){
				if(self::$user['role_id']==1 &&$REQUEST['t']=='assistant'){
					$url = SalemanagerHelper::redirectUrl(self::$user['role_id'])."noassistant";
				}else{
					$url = SalemanagerHelper::redirectUrl(self::$user['role_id'])."noSaleMan";
				}
				
				$this->redirect($url);
				//$this->display('noSaleMan');
			}
			$this->display();
		}
	}
	
	/**
	 * 删除业务员
	 */
	public function delSaleMan(){
		$REQUEST = ( array ) I ( 'request.' );
		$saleId=think_ucenter_decrypt($REQUEST['userid']);
		if(is_numeric($saleId)){
			$re = self::del($this->saleModel, array('id'=>$saleId), array('status'=>C('SALEMANAGER_STATUS.DELETED')));
			if($re!==false){
				responseJson($re['status'],$re['msg'],$re['data']);
			}else{
				responseJson($re['status'],$re['msg'],$re['data']);
			}
				
		}else{
			responseJson($re['status'],$re['msg'],$re['data']);
		}
	}
	
	/**
	 * 添加用户(包括添加业务员，地总，省总)
	 * @param mixed $post        	
	 */
	public function addSaleman() {
		if (IS_AJAX) {
			$REQUEST = ( array ) I ( 'request.' );
			$data ['phone'] = intval($REQUEST ['phone']);
			$data ['password'] = think_ucenter_md5(C('SALE_MANAGER.INIT_PASSWORD'),UC_AUTH_KEY);
			
			$data ['card_number'] = $REQUEST ['card_number'];
			$data ['sex'] = $REQUEST ['sex'];
			$data ['age'] = $REQUEST ['age'];
			$data ['add_time'] = time();
			$data ['superior_id'] = self::$user['id']; //当前用户uid
			//大区经理特殊处理
			if(self::$user['role_id']==1 &&$REQUEST['t']=='assistant'){ //如果是大区经理的助理
				$data ['role_id']=11;
				$data ['real_name'] = $REQUEST ['real_name'].'助理';
			}else{
				$data ['real_name'] = $REQUEST ['real_name'].SalemanagerHelper::getLowerRoleName(self::$user['role_id']);
				$data ['role_id'] = $this->lowerRoleId; // 根据type
			}
			$data ['manage_locations'] = $REQUEST ['belong_locations'];
			$re = SalemanagerHelper::upload();//附件上传
			if($re){
				foreach ($re as $key=>$url){
					$data[$key] = $url;
				}
			}
			//如果是添加大区经理助理
			if(self::$user['role_id']==1 &&$REQUEST['t']=='assistant'){
				$model = new Model();
				$model->startTrans();
				$result = $this->saleModel->add ( $data );
				if($result==false){
					$model->rollback();
					responseJson($result['status'],$result['msg'],$result['data']);
				}
				//修改对应大区经理的省总
				$clinics=json_decode(html_entity_decode($REQUEST['province']),true);
				$map['id']=array('IN',implode(',',$clinics));
				$re = D('saleman')->where($map)->setField(array('superior_id'=>$result['data']['id']));
				if($re==false){
					$model->rollback();
					responseJson($result['status'],$result['msg'],$result['data']);
				}
				$model->commit();
			}else{
				$result = $this->saleModel->add ( $data );
			}
			responseJson($result['status'],$result['msg'],$result['data']);
			
		} else {
			//大区经理助理处理
			if(self::$user['role_id']==1 && I('get.t')=='assistant'){
				//获取该大区经理下面的所有省总
				$childrens = WorksummerHelper::getAllChildrenId(self::$user['id']);
				$map['id']  = array('in',$childrens);
				$map['role_id']=2;
				$provinces = D('saleman')->where($map)->select();
				$this->assign('provinces',$provinces);
				$this->display('addassistant');exit;
			}
			$this->display();
		}
	}
	
	/**
	 * 业务员详细信息
	 */
	public  function saleInfos(){
		//self::detail();
		//$this->display();
		$REQUEST = ( array ) I ( 'request.' );
		$userid =think_ucenter_decrypt($REQUEST['userId']);
		if(is_numeric($userid)){
			$user = $this->saleModel->findInfobyCondition(array("id"=>$userid));
			$user[0]['id']=$userid;
			$user[0]['card_number']=substr_replace($user[0]['card_number'],'*********',3,9);
			$this->assign('user',$user[0]);
			$this->display();
		}else{
			$this->assign('status','validatefail');
			$this->assign('msg','参数错误！');
			$this->display();
			//$this->error('错误!');
		}
	}
	
	/**
	 *   支持审批
	 */
	public function appliceSupport(){
		if(IS_POST){
			self::saleApply();
		}else{
			if(self::$user['role_id']==2 ||self::$user['role_id']==1){
				$this->assign('app_type',C('PROVICE_APPLY_NAME'));
			}else{
				$this->assign('app_type',C('SALEMANAGER_APPLY_NAME'));
			}
			
			$this->display();
		}
	
	}
	
	
	
	
	
	
	
	
	/**
	 * 每个用户详细信息的获取
	 */
	private function detail(){
		$REQUEST = ( array ) I ( 'request.' );
		$userid =think_ucenter_decrypt($REQUEST['userId']);
		if(is_numeric($userid)){
			$user = $this->saleModel->findInfobyCondition(array("id"=>$userid));
			$tmpArray = explode('/', $user[0]['head_img']);
			//$re = $this->produce($user[0]['head_img'], $tmpArray[count($tmpArray)-1]);
			$user[0]['id']=$userid;
			$this->assign('user',$user[0]);
			$this->display();
		}else{
			$this->assign('status','validatefail');
			$this->assign('msg','参数错误！');
			$this->display();
			//$this->error('错误!');
		}
	}
	
	
	
	/**
	 * 修改用户(更换下级用户)
	 * @param mixed $post         	
	 */
	public function edit() {
		$REQUEST = ( array ) I ( 'request.' );
		$REQUEST['id']=think_ucenter_decrypt($REQUEST['id']);
		if (IS_POST) {
			if(is_numeric($REQUEST['id'])){
				$data ['phone'] = intval($REQUEST ['phone']);
				$data ['password'] = think_ucenter_md5(C('SALE_MANAGER.INIT_PASSWORD'),UC_AUTH_KEY);
				$data ['real_name'] = $REQUEST ['real_name'];
				$data ['card_number'] = $REQUEST ['card_number'];
				$data ['sex'] = $REQUEST ['sex'];
				$data ['age'] = $REQUEST ['age'];
										//所属县
				$result = $this->saleModel->updateById ($REQUEST['id'],$data );
			}else{
				$result =array(
						'status'=>C('SALE_MANAGER.ID_VALIDATE_FAIL_STATUS'),
						'msg'=>C('SALE_MANAGER.ID_VALIDATE_FAIL_MSG'),
				);
			}
			responseJson ( $result );
		}else{
			$this->display ();
		}
	}
	
	/**
	 * 修改个人资料
	 * @param mixed $post
	 */
	public function editMyself(){
		//图片上传操作
		if(IS_AJAX){
			$re = SalemanagerHelper::upload();//附件上传(包括身份证正反面，头像),字段名称跟数据库字段一样即可
			if($re){
				foreach ($re as $key=>$url){
					$data[$key] = $url;
				}
			}
			$data ['id'] = self::$user['id'];
			$result = $this->saleModel->updateField (self::$user['id'],$data );
			//资料更新，更新session
			if($result['status']==1){
				$tuser = $this->saleModel->findInfobyCondition(array('id'=>self::$user['id']));
				$sesseionmane = C('SESSION_PREFIX').'_salemanager';
				session($sesseionmane,$tuser[0]);
			}
			responseJson ($result['status'],$result['msg'],$result['data'] );
		}
		
		
		//表单提交操作
		if (IS_POST) {
			$REQUEST = ( array ) I ( 'request.' );
			$data ['id'] = self::$user['id'];
			$data ['phone'] = intval($REQUEST ['phone']);
			$data ['real_name'] = $REQUEST ['real_name'];
			$data ['card_number'] = $REQUEST ['card_number'];
			$data ['sex'] = $REQUEST ['sex'];
			$data ['age'] = $REQUEST ['age'];
		
// 			$re = SalemanagerHelper::upload();//附件上传(包括身份证正反面，头像),字段名称跟数据库字段一样即可
// 			if($re){
// 				foreach ($re as $key=>$url){
// 					$data[$key] = $url;
// 				}
// 			}
			$result = $this->saleModel->updateField (self::$user['id'],$data );
			//资料更新，更新session
			if($result['status']==1){
				$tuser = $this->saleModel->findInfobyCondition(array('id'=>self::$user['id']));
				$sesseionmane = C('SESSION_PREFIX').'_salemanager';
				session($sesseionmane,$tuser[0]);
			}
			if(!empty($REQUEST['f']) && $REQUEST['f']=='json'){
				responseJson ($re['status'],$re['msg'],$re['data'] );
			}else{
				return $result;
			}
		} else {
			$this->display ();
		}
	}
	
	/**
	 * 更换下级
	 * @param mixed $post
	 */
	public function changeLower($model,$where,$data){
		if(empty($where)){
			return false;
		}
		return $model->updateFieldByCondition($where,$data);
	}
	
	/**
	 * 消息列表
	 * @param mixed $post
	 */
	public function infoList(){
		$REQUEST = ( array ) I ( 'request.' );
			$totalMsg = $this->noticeModel->findInfobyCondition(array('accept_saleman_id'=>self::$user['id']),NULL,NULL,'send_time desc');
			//用于统计
			//$totalMsg1 = $this->noticeModel->findInfobyCondition(array('is_look'=>0,'accept_saleman_id'=>self::$user['id']),NULL,NULL,'send_time desc');
			$lowers = $this->saleModel->findInfobyCondition(array('superior_id'=>self::$user['id']));
			$lowerIds=array();
			foreach ($lowers as $lk=>$lv){
				$lowerIds[$lv['id']]=$lv['id'];
			}
			$superMsg=array();
			$lowerMsg=array();
			$supertotal=0;
			foreach ($totalMsg as $k=>$v){
				if($v['accept_saleman_id']==self::$user['id'] && $v['type']==1){
					$u = $this->saleModel->findById($v['send_user_id']);
					$v['send_time_data'] = SalemanagerHelper::linfolist($v['send_time']);
					$v['date_time'] = SalemanagerHelper::linfolist($v['send_time']);
					$v['send_name']= $u['real_name'];
					$v['head_img']= $u['head_img'];
					$fivesDays = strtotime("-5 day");
					if($v['send_time']>$fivesDays){
						if($v['is_look']==0){
							$supertotal++;
							array_push($superMsg,$v);
						}
						
					}
				}
				if(array_key_exists($v['send_user_id'], $lowerIds)){
					$u = $this->saleModel->findById($v['send_user_id']);
					$v['send_time_data'] = SalemanagerHelper::linfolist($v['send_time']);
					//$v['send_time '] = \Home\Common\SalemanagerHelper::timeFormat($v['send_time']);
					$v['send_name']= $u['real_name'];
					//array_push($lowerMsg,$v);
					if($v['is_look']==0){
						array_push($lowerMsg,$v);
					}else{
						unset($totalMsg[$k]);
					}
				}
			}
			//对下级消息进行按人员统计
				$result =   [];  //初始化一个数组
				$low=0;
				foreach($lowerMsg as $k=>$v){
					if($v['is_look']==0 ){
						$low=$low+1;
						$result[$v['send_user_id']][] =$v;
					}
				      //根据send_user_id 进行数组重新赋值
				}
			$this->assign('supercount',$supertotal);//上级条数
			$this->assign('lowercount', empty($low)?0:$low);//下级条数
			$this->assign('supertime',$superMsg[0]['send_time_data']);//上级最后时间
			$this->assign('lowertime', $lowerMsg[0]['send_time_data']);//下级最后时间
// 			$this->assign('supercount',count($superMsg));//上级条数
			//$this->assign('result', $result);//按人员分组显示数据
			if(!empty($REQUEST['tag'])){
				if($REQUEST['tag']=='upinfo'){
					$this->assign('infos',$superMsg);//上级消息
					$this->display('upinfolist');
					exit;
				}
				if($REQUEST['tag']=='downinfo'){
					$this->assign('info',$lowerMsg);//下级消息
					$this->assign('infores',$result);//下级消息
				}
			}else{
				if(!empty($superMsg)){
					$this->assign('lastupinfo',$superMsg[0]['send_name']);//上级最近时间的一条
				}
				if(!empty($lowerMsg)){
					$this->assign('lastdowninfo',$lowerMsg[0]['send_name']);//下级最近时间的一条
				}
				$this->assign('super',$superMsg);//上级消息
				$this->assign('lower',$lowerMsg);//下级消息
				$this->assign('total', $supertotal+(empty($low)?0:$low));//下级条数
			}
			//系统消息
			$infos = D('system_message')->where('accept_id=0 or accept_id='.self::$user['id'])->order('date desc')->select();
			foreach ($infos as $k=>$v){
				$infos[$k]['date_time'] = SalemanagerHelper::syslinfolist($v['date']);
			}
			
			$this->assign('sysmessage',$infos[0]['date_time']);//系统消息
			$this->assign('systitle',$infos[0]['title']);//系统消息
			$this->assign('systeminfos',$infos);//上级消息
			$this->display();
	}
	
	//系统消息
	public function sysinfolist(){
		$REQUEST = ( array ) I ( 'request.' );
		if(!empty($REQUEST['id'])){
			$infos = D('system_message')->where('id='.$REQUEST['id'])->find();
			$this->assign('infos',$infos);
			$this->display('sysmessagedetail');
		}else{
			$infos = D('system_message')->where('accept_id=0 or accept_id='.self::$user['id'])->order('date desc')->select();
			foreach ($infos as $k=>$v){
				$infos[$k]['date_time'] = SalemanagerHelper::syslinfolist($v['date']);
			}
			$this->assign('sysmessage',$infos[0]['date_time']);//系统消息
			$this->assign('systitle',$infos[0]['title']);//系统消息
			$this->assign('infos',$infos);//系统消息
			$this->display();
		}
		
	}
	//上级消息详情
	public function upinfolist(){
		$REQUEST = ( array ) I ( 'request.' );
		if(!empty($REQUEST['id'])){
			$infos = D('saleman_notice')->where('id='.$REQUEST['id'])->find();
			
			$this->assign('infos',$infos);
			$this->noticeModel->updateById($REQUEST['id'], array('is_look'=>'1'));
			$this->display('upsysmessagedetail');
		}
	
	}
	/**
	 * 申请审批
	 */
	private function saleApply(){
		if(IS_POST){
			$REQUEST = ( array ) I ( 'request.' );
			$data['saleman_id']= self::$user['id'];
			$tmp = SalemanagerHelper::getSaleHeadById(self::$user['superior_id'],0);
			$data['apply_content']= $REQUEST['apply_content'];
			$data['type']= $REQUEST['type'];
			$data['superior_id']= self::$user['superior_id'];
			$data['apply_time'] = time();
			$data['copy_id'] = $tmp['id'];
			$re =$this->saleApplyModel->add($data);
			responseJson($re['status'], $re['msg'],$re['data']);
		}		
	}
	/**
	 * 审批记录
	 */
	public function approvalRecord(){
		$REQUEST = ( array ) I ( 'request.' );
		if(IS_AJAX){
			if(!empty($REQUEST['pageStart'])){
				if($REQUEST['itemIndex']==0){
					//我发起的下拉刷新
					unset($applyByMe);
					$applyByMe = $this->saleApplyModel->findInfobyCondition(array('saleman_id'=>self::$user['id']),C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart'],'apply_time desc');
				}
				if($REQUEST['itemIndex']==1){
					//待审批下拉刷新
					unset($applyByMe);
					if(self::$user['role_id']==7 || self::$user['role_id']==0){
						$applyByMe = $this->saleApplyModel->findInfobyCondition(array('reply_status'=>'0'),C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart'],'apply_time desc');
						
					}else{
						$applyByMe = $this->saleApplyModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'reply_status'=>'0'),C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart'],'apply_time desc');
						
					}
				}
				if($REQUEST['itemIndex']==2){
					//已审批刷新
					unset($applyByMe);
					if(self::$user['role_id']==7 || self::$user['role_id']==0){
						$applyByMe = $this->saleApplyModel->findInfobyCondition('( reply_status<>0 and type<>8 ) or (type=8 and reply_status=2 )',C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart'],'apply_time desc');
						
					}else{
						$applyByMe = $this->saleApplyModel->findInfobyCondition('(superior_id='.self::$user["id"].' and reply_status<>0 and type<>8 ) or (type=8 and reply_status=2 and superior_id='.self::$user["id"].')',C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart'],'apply_time desc');
					}
				}
				foreach($applyByMe as $k=>$v){
					$applyByMe[$k]['id']=think_ucenter_encrypt($v['id']);
					if($REQUEST['itemIndex']!=0){
						$applyByMe[$k]['apply_name'] = $this->saleModel->findInfobyCondition(array('id'=>$applyByMe[$k]['saleman_id']))[0]['real_name'];
					}else{
						$applyByMe[$k]['apply_name'] = self::$user['real_name'];
					}
					$applyByMe[$k]['apply_cstatus']=SalemanagerHelper::convertApplyStatus($applyByMe[$k]['reply_status']);//同意，拒绝，待审批
					$applyByMe[$k]['ctype']=SalemanagerHelper::traMetting($applyByMe[$k]['type']); //申请类型
					$applyByMe[$k]['apply_time']=date ( 'Y-m-d', $applyByMe[$k]['apply_time'] ); //申请时间
					
				}
			}	
			responseJson(200, '下拉刷新成功',$applyByMe);
		}else{
			//我发起的
			$applyByMe = $this->saleApplyModel->findInfobyCondition(array('saleman_id'=>self::$user['id']),C('SALEMANAGER_PAGE_NUM'),0,'apply_time desc');
			foreach($applyByMe as $k=>$v){
				$applyByMe[$k]['id']=think_ucenter_encrypt($v['id']);
				$applyByMe[$k]['apply_name'] = self::$user['real_name'];
				$applyByMe[$k]['apply_cstatus']=SalemanagerHelper::convertApplyStatus($applyByMe[$k]['reply_status']);//同意，拒绝，待审批
				$applyByMe[$k]['ctype']=SalemanagerHelper::traMetting($applyByMe[$k]['type']); //申请类型
				if($applyByMe[$k]['ctype']=='离职申请' && $applyByMe[$k]['reply_status']!=0){
					unset($applyByMe[$k]);
				}
			}
			//$Approve = $this->saleApplyModel->findInfobyCondition(array('superior_id'=>self::$user['id']),null,null,'apply_time desc');
			$waitApp = array();
			$proved	= array();
			if(self::$user['role_id']==7 || self::$user['role_id']==0){
				$proved = $this->saleApplyModel->findInfobyCondition('( reply_status<>0 and type<>8) or (type=8 and reply_status=2)',C('SALEMANAGER_PAGE_NUM'),0,'apply_time desc');
					
			}else{
				$proved = $this->saleApplyModel->findInfobyCondition('(superior_id='.self::$user["id"].' and reply_status<>0 and type<>8) or (type=8 and reply_status=2  and superior_id='.self::$user["id"].')',C('SALEMANAGER_PAGE_NUM'),0,'apply_time desc');
			}
			//echo M()->getlastsql();
			if(self::$user['role_id']==7||self::$user['role_id']==0){
				$waitApp = $this->saleApplyModel->findInfobyCondition(array('reply_status'=>0),C('SALEMANAGER_PAGE_NUM'),0,'apply_time desc');
					
			}else{
				$waitApp = $this->saleApplyModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'reply_status'=>0),C('SALEMANAGER_PAGE_NUM'),0,'apply_time desc');
			}
			foreach($proved as $k=>$v){//已审批
				$proved[$k]['id']=think_ucenter_encrypt($v['id']);
				$proved[$k]['apply_name'] = $this->saleModel->findInfobyCondition(array('id'=>$proved[$k]['saleman_id']))[0]['real_name'];
				$proved[$k]['apply_cstatus']=SalemanagerHelper::convertApplyStatus($proved[$k]['reply_status']);
				$proved[$k]['ctype']=SalemanagerHelper::traMetting($proved[$k]['type']);
			}
			foreach($waitApp as $k=>$v){//待审批
				$waitApp[$k]['id']=think_ucenter_encrypt($v['id']);
				$waitApp[$k]['apply_name'] = $this->saleModel->findInfobyCondition(array('id'=>$waitApp[$k]['saleman_id']))[0]['real_name'];
				$waitApp[$k]['apply_cstatus']=SalemanagerHelper::convertApplyStatus($waitApp[$k]['reply_status']);
				$waitApp[$k]['ctype']=SalemanagerHelper::traMetting($waitApp[$k]['type']);
			}
			$this->assign('applyByMe',$applyByMe);
			$this->assign('approved',$proved);
			$this->assign('waitProve',$waitApp);
			$this->display();
		}
		
	}
	
	/**
	 * 审批详情
	 */
	public function approvalInfo(){
		$REQUEST = ( array ) I ( 'request.' );
		$applyId =think_ucenter_decrypt($REQUEST['applyId']);
		if(is_numeric($applyId)){
			$appinfo= $this->saleApplyModel->findById($applyId);
			$appinfo['apply_time'] = date ( 'Y-m-d H:i:s', $appinfo['apply_time'] );
			$applyUser = $this->saleModel->findById($appinfo['saleman_id']);
			$appinfo['apply_cstatus']=SalemanagerHelper::convertApplyStatus($appinfo['reply_status']);
			$appinfo['ctype']=SalemanagerHelper::traMetting($appinfo['type']);
			$appinfo['apply_name'] = $applyUser['real_name'];
			$appinfo['ids'] =think_ucenter_encrypt($appinfo['id']);
			$appinfo['saleman_ids'] =think_ucenter_encrypt($appinfo['saleman_id']);
			$this->assign('appinfo',$appinfo);
			$this->display();
		}else{
			$this->assign('status','error');
			$this->assign('msg','参数错误！');
			$this->display();
		}
	}
	
	/**
	 *审批操作
	 */
	public function approvalOpinionOne(){
		$REQUEST = ( array ) I ( 'request.' );
		if(IS_AJAX){
			$data['reply_status']=$REQUEST['reply_status'];
			$data['reply_content']=$REQUEST['reply_content'];
			$data['reply_time']=$REQUEST['reply_time'];
			$re = $this->saleApplyModel->updateField(think_ucenter_decrypt($REQUEST['applyId']), $data);
			 //$this->redirect('/home/SaleManager/County/index');
			responseJson($re['status'], $re['msg'],$re['data']);
		}else{
			$applyId =think_ucenter_decrypt($REQUEST['applyId']);
			if(is_numeric($applyId)){
				$appinfo= $this->saleApplyModel->findById($applyId);
				$applyUser = $this->saleModel->findById($appinfo['saleman_id']);
				$appinfo['apply_time'] = date ( 'Y-m-d H:i:s', $appinfo['apply_time'] );
				$appinfo['apply_cstatus']=SalemanagerHelper::convertApplyStatus($appinfo['reply_status']);
				$appinfo['ctype']=SalemanagerHelper::traMetting($appinfo['type']);
				$appinfo['apply_name'] = $applyUser['real_name'];
				$appinfo['ids'] =think_ucenter_encrypt($appinfo['id']);
				$appinfo['saleman_ids'] =think_ucenter_encrypt($appinfo['saleman_id']);
				$this->assign('appinfo',$appinfo);
				$this->assign('appuser',$applyUser);
				if($appinfo['ctype']=='离职申请'){
					if($applyUser['role_id']==11){
						$userList= $this->saleModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'role_id'=>11,'status'=>C('SALEMANAGER_STATUS.NORMAL')),null,null,'id desc');
						foreach ($userList as $key=>$value){
							$userList[$key]['id'] = think_ucenter_encrypt($userList[$key]['id']);
						}
					}else{
						$userList = self::getLower();
					}
					foreach ($userList as $k=>$v){
						if($v['id']==think_ucenter_encrypt($appinfo['saleman_id'])){
							unset($userList[$k]);
							continue;
						}
					}
					$this->assign('lists',$userList);
					$this->display('approveopinion');
				}else{
					$this->display();
				}
				
			}else{
				$this->assign('status','error');
				$this->assign('msg','参数错误！');
				$this->display();
			}
		}
		
	}
	
	//离职申请处理
	public function approveOpinion(){
		$REQUEST = ( array ) I ( 'request.' );
		if(IS_AJAX){
			if($REQUEST['reply_status']==2){
				$data['reply_status']=$REQUEST['reply_status'];//不同意
				$reApplyStatus = $this->saleApplyModel->updateField((think_ucenter_decrypt($REQUEST['applyId'])), $data);
				if($reApplyStatus['status']==1){
					responseJson(1, '操作成功！');
				}else{
					responseJson(1, '操作失败！');
				}
			}
			$changeUser = think_ucenter_decrypt($REQUEST['change_apply']);//交接人
			$originUser = $REQUEST['origin_user'];//离职人
			$data['reply_status']=$REQUEST['reply_status'];//同意，不同意
			$reApplyStatus = $this->saleApplyModel->updateField((think_ucenter_decrypt($REQUEST['applyId'])), $data);
			$data['saleman_id'] = $changeUser;
			$model = new Model();
			$model->startTrans();
			$re = self::changeLower($this->clinicMod, array('saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user'])), $data);//更换诊所
			$applyCondition=array(
					'saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			$apply2Condition=array(
					'superior_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			//申请支持更换
			$appRe = $this->saleApplyModel->updateFieldByCondition($applyCondition, array("saleman_id"=>$data['saleman_id']));
		
			$app2Re = $this->saleApplyModel->updateFieldByCondition($apply2Condition, array("superior_id"=>$data['saleman_id']));
			
			$noticeCondition=array(
					'send_user_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			$notice2Condition=array(
					'accept_saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			//消息更换主人
			$noticeRe = $this->noticeModel->updateFieldByCondition($noticeCondition, array("send_user_id"=>$data['saleman_id']));
			
			$notice2Re = $this->noticeModel->updateFieldByCondition($notice2Condition, array("accept_saleman_id"=>$data['saleman_id']));
			
			//订单更换
			$orderCondition=array(
					'send_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			$order2Condition=array(
					'saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			$orderRe = $this->saleOrderModel->updateFieldByCondition($orderCondition, array("send_id"=>$data['saleman_id']));
			
			$order2Re = $this->saleOrderModel->updateFieldByCondition($order2Condition, array("saleman_id"=>$data['saleman_id']));
			
			//签到更换
			$signCondition=array(
					'saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			$signRe = $this->saleSignModel->updateFieldByCondition($signCondition, array("saleman_id"=>$data['saleman_id']));
			
			//工作计划更换
			$tripCondition=array(
					'saleman_id'=>think_ucenter_decrypt($REQUEST['origin_user']),
			);
			$tripRe = $this->saleTripModel->updateFieldByCondition($tripCondition, array("saleman_id"=>$data['saleman_id']));
			
			//被替换的人设置为禁用
			$saleResult = $this->saleModel->updateField (think_ucenter_decrypt($REQUEST['origin_user']),array('status'=>C('SALEMANAGER_STATUS.FORBIDDEN')) );
			
			if($re['status']==1&&$appRe['status']==1&& $app2Re['status']==1 &&$saleResult['status']==1&&$noticeRe['status']==1&&$notice2Re['status']==1&&$orderRe['status']==1&&$order2Re['status']==1&&$signRe['status']==1&&$tripRe['status']==1&&$reApplyStatus['status']==1){
				$model->commit();
				//根据角色跳转
				responseJson(1, '交接成功！');
			}else{
				$model->rollback();
				responseJson(2, '交接失败！');
			}
			responseJson(2, '交接失败！');
		}
	
	}
	/**
	 * 删除操作，假删除
	 * @param mixed $post
	 */
	public function del($model,$where,$data){
		if(empty($where)){
			return false;
		}
		return $model->updateFieldByCondition($where,$data);
	}
	
	/**
	 * 订单（当前用户下级收到的订单，包括业务员收到订单，县总收到订单）
	 * @param mixed $post
	 */
	public function orderlist(){
		//获取当前用户的下级收到订单
		$REQUEST = ( array ) I ( 'request.' );
		$status = $REQUEST['status'];
		if(isset($REQUEST['status']) && $REQUEST['status']!=""){
			$sql=' and s.status='.$status;
		}
		if(IS_AJAX){
			if(isset($REQUEST['pageStart'])){
				//总部，销售部省总订单
				if(self::$user['role_id']==C('SALE_MANAGER.SALE_HEAD')||self::$user['role_id']==C('SALE_MANAGER.SALE_DEPARTMENT')){
// 					$re = M('saleman')->field('s.id as oid,s.add_date,s.status,s.total_price,ymt_saleman.role_id,ymt_saleman.real_name,ymt_saleman.id as sid,ymt_saleman.head_img')->where('ymt_saleman.role_id in ('.C('SALE_MANAGER.SALE_DEPARTMENT').','.C('SALE_MANAGER.SALE_PROVINCE').')'.$sql.' and s.type=1')
// 					->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')->limit($REQUEST['pageStart'],C('SALEMANAGER_PAGE_NUM'))
// 					->select();
					$re = M('saleman')->field('s.id as oid,s.add_date,s.status,s.total_price,ymt_saleman.role_id,ymt_saleman.real_name,ymt_saleman.id as sid,ymt_saleman.head_img')->where('ymt_saleman.role_id=2'.$sql.' and s.type=1')
					->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')->limit($REQUEST['pageStart'],C('SALEMANAGER_PAGE_NUM'))
					->select();
				}else{
					$re = M('saleman')->field('s.id as oid,s.add_date,s.status,s.total_price,ymt_saleman.role_id,ymt_saleman.real_name,ymt_saleman.id as sid,ymt_saleman.head_img')->where('ymt_saleman.superior_id='.self::$user['id'].$sql.' and s.type=1')
					->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')->limit($REQUEST['pageStart'],C('SALEMANAGER_PAGE_NUM'))
					->select();
				}
 				//echo M()->getLastSql();
				foreach ($re as $key=>$value){
					$detail_id = M("saleman_order_detail")->where("order_id=".$value['oid'])->getField("drugs_id");
					$re[$key]['head_img'] =M("drugs")->where("id=".$detail_id)->getField("cover_img");
					$re[$key]['id']=think_ucenter_encrypt($value['id']);
					$re[$key]['osid']=think_ucenter_encrypt($value['oid']);
					$re[$key]['status']=SalemanagerHelper::convertOrderStatus($value['status']);
					$re[$key]['role_name']=SalemanagerHelper::getRoleName($value['role_id']);
				}
			}
			//echo M()->getLastSql();
			responseJson(200,'刷新成功',$re);
		}else{
			$sumPrice=0;
			//总部，销售部省总订单总数
			if(self::$user['role_id']==C('SALE_MANAGER.SALE_HEAD')||self::$user['role_id']==C('SALE_MANAGER.SALE_DEPARTMENT')){
// 				$res = M('saleman')->field('s.total_price')->where('s.type=1')
// 				->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')
// 				->select();
				$res = M('saleman')->field('s.total_price')->where('s.type=1 and ymt_saleman.role_id=2')
				->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')
				->select();
			
			}else{
				//大区经理,省总,县总，地总订单总数
				$res = M('saleman')->field('s.total_price')->where('ymt_saleman.superior_id='.self::$user['id'].$sql.' and s.type=1')
				->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')
				->select();
			}
			foreach ($res as $key=>$value){
				$sumPrice=$sumPrice+intval($value['total_price']);
			}
	
			//总部，销售部省总订单
			if(self::$user['role_id']==C('SALE_MANAGER.SALE_HEAD')||self::$user['role_id']==C('SALE_MANAGER.SALE_DEPARTMENT')){
// 				$re = M('saleman')->field('s.id as oid,s.add_date,s.status,s.total_price,ymt_saleman.role_id,ymt_saleman.head_img,ymt_saleman.real_name,ymt_saleman.id as sid')->where('s.type=1')
// 				->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')->limit(0,C('SALEMANAGER_PAGE_NUM'))
// 				->select();
				$re = M('saleman')->field('s.id as oid,s.add_date,s.status,s.total_price,ymt_saleman.role_id,ymt_saleman.head_img,ymt_saleman.real_name,ymt_saleman.id as sid')->where('s.type=1 and ymt_saleman.role_id=2')
				->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')->limit(0,C('SALEMANAGER_PAGE_NUM'))
				->select();
			}else{
				//大区经理省总订单
				$re = M('saleman')->field('s.id as oid,s.add_date,s.status,s.total_price,ymt_saleman.role_id,ymt_saleman.head_img,ymt_saleman.real_name,ymt_saleman.id as sid')->where('ymt_saleman.superior_id='.self::$user['id'].$sql.' and s.type=1')
				->join('inner join ymt_saleman_order  s ON s.send_id = ymt_saleman.id')->order('s.add_date desc')->limit(0,C('SALEMANAGER_PAGE_NUM'))
				->select();
			}
			
			foreach ($re as $key=>$value){
				$detail_id = M("saleman_order_detail")->where("order_id=".$value['oid'])->getField("drugs_id");
				$re[$key]['head_img'] =M("drugs")->where("id=".$detail_id)->getField("cover_img");
				$re[$key]['id']=think_ucenter_encrypt($value['id']);
				$re[$key]['osid']=think_ucenter_encrypt($value['oid']);
				$re[$key]['status']=SalemanagerHelper::convertOrderStatus($value['status']);
				$re[$key]['role_name']=SalemanagerHelper::getRoleName($value['role_id']);
			}
			$this->assign('title',SalemanagerHelper::getRoleTitle(self::$user['role_id']));
			$this->assign('toralprice',$sumPrice);
			$this->assign('total',count($res));
			$this->assign('lists',$re);
			$this->display('SaleManager/County/clinicorder');
		}
		
	}
	
	public function orderInfo(){
		$REQUEST = ( array ) I ( 'request.' );
		if(IS_POST){
			
		}else{
			if(!empty($REQUEST['oid'])){
				//print_r($REQUEST['oid']);
				$REQUEST['oid'] =str_replace(" ","+",$REQUEST['oid']);
				$res = D('saleman_order')->where(array('id'=>think_ucenter_decrypt($REQUEST['oid'])))->find();
				$info = M('saleman_order_detail')->where(array('order_id'=>think_ucenter_decrypt($REQUEST['oid'])))->select();
				foreach ($info as $k=>$v){
					$total+=$v['total_price'];
				}
			}
			$this->assign('address',empty($res['address'])?"暂无":$res['address']);
			$this->assign('total',$total);
			$this->assign('oid',$REQUEST['oid']);
			$this->assign('status',$REQUEST['status']);
			$this->assign('info',$info);
			$this->display();
		}	
	}
	//完成已完成订单
	public function completOrder(){
		$REQUEST = ( array ) I ( 'request.' );
		if(IS_AJAX){
			if(!empty($REQUEST['oid'])){
				$REQUEST['oid'] =str_replace(" ","+",$REQUEST['oid']);
				$re = M('saleman_order')->where('id='.think_ucenter_decrypt($REQUEST['oid']))->setField(array('status'=>1));
				if($re!==false){
					responseJson(200, '操作成功');
				}else{
					responseJson(2001, '操作失败');
				}
			}
			responseJson(2001, '参数错误');
		}
	}
	
	/**
	 * 诊所分配任务
	 */
	public function distribution(){
		if(IS_POST){
			$REQUEST = ( array ) I ( 'request.' );
			$salemanId=$REQUEST['salemanId'];
			$clinics=json_decode(html_entity_decode($REQUEST['clinics']),true);
			if(empty($salemanId)||empty($clinics)){
				responseJson(200, '诊所或分配人员为空');
			}
			//1.更换诊所负责人
			$model = new Model();
			$model->startTrans();
			$map['id']=array('IN',implode(',',$clinics));
			$re = D('clinic')->where($map)->setField(array('saleman_id'=>$salemanId));
			//2.诊所未完成的订单更换
			$orderMap['type']=0;
			$orderMap['status']=array('NEQ',1);
			$orderMap['send_id']=array('IN',implode(',',$clinics));
			$orderResult = D('saleman_order')->where($orderMap)->setField(array('saleman_id'=>$salemanId));
			//echo M()->getlastsql();
			if($re!==false&&$orderResult!==false){
				$model->commit();
				responseJson(200, '分配成功');
			}else{
				responseJson(200, '分配失败');
				$model->rollback();
				
			}
			
			//2.更换诊所未完成订单给相关人员
		}else{
			//获取该用户及下属所有的诊所，需要获取诊所名称，业务员名称，诊所地址
			$salemans=D('saleman')->where('superior_id='.self::$user['id'].' and role_id='.$this->lowerRoleId.' and status='.C('SALEMANAGER_STATUS.NORMAL').' or id='.self::$user['id'])->select();
			$ids=array_column($salemans, 'id');
			$map['saleman_id']=array('IN',implode(',',$ids));
			$map['is_delete']=1;
			$clinics=D('clinic')->where($map)->field('clinic_name,clinic_address,saleman_id,type,id')->order('type desc')->select();
			//$names = array_reduce($salemans, create_function('$v,$w', '$v[$w["id"]]=$w["real_name"];$v[$w["role_id[]]=$w["role_id"];return $v;')); 
			$this->assign('lists',$salemans);//诊所负责人
			$this->assign('clinics',$clinics);//诊所列表
			//$this->assign('names',$salemans);//诊所负责人名称
			$this->display();
		}
	}

    /**
     * 消息推送(上级通知)
     */
    public function pushhistory($role_id){
        if(IS_POST){
            $rrole_id=I('post.rrole_id');
            $title=I('post.title');
            $txt=I('post.txt');
            $historymodel=M('PushHistory');
            //开启事务
            $historymodel->startTrans();
            $result=true;
            foreach ($rrole_id as  $k=>$val) {
                //对所有诊所进行推送
                if($val==8){
                    //1.查询所有诊所负责人且允许进行极光推送的register_id的信息
                    $info=M('Doctor')->alias('a')->field('a.id,b.register_id')->where(array('a.is_manager'=>1,'a.is_allow'=>0))->join('JOIN __DOCTOR_PUSH__ b on a.id=b.doctor_id')->select();
                    //2.查询出所有诊所中不进行极光推送的信息
                    $doctor=M('Doctor')->field('id')->where(array('is_manager'=>1))->select();
                    //根据条件判断进行极光推送
                    $register_id=array();
                    $uid=array();
                    if($info){
                        //1.1允许进行极光推送
                        //1.1.1查询出诊所负责人的register_id
                        foreach ($info as  $k=>$val) {

                            $register_id[$k]=$val['register_id'];
                            $uid[$k]['id']=$val['id'];
                        }
                        //1.1.2向医生端推送信息
                        sendNotifyDoctor($register_id,$title,$txt);
                    }else{
                        //1.2不允许进行极光推送
                        foreach ($doctor as  $k=>$val) {
                            $uid[$k]['id']=$val['id'];
                        }
                    }
                    //1.3进行表存储，添加到信息表ymt_system_message和ymt_push_history
//                //开启事务
//                $historymodel->startTrans();
                    //向数据库表中插入数据
                    $mydata = array(
                        'title' =>$title,
                        'content'=>$txt,
                        'type'=>8,
                        'user_id'=>self::$user['id']
                    );
                    $re=$historymodel->add( $mydata);
                    if($re===false){
                        $historymodel->rollback();
                        $this->ajaxReturn(array('code' => 1,'msg'=>'发送失败'));
                    }
                    $data=array();
                    foreach ($uid as  $k=>$val) {
                        $data[$k]['accept_id']=$val['id'];
                        $data[$k]['title']=$title;
                        $data[$k]['content']=$txt;
                        $data[$k]['type']=2;//所有诊所
                    }
                    //向数据库表中插入数据
                    $result=M('SystemMessage')->addALL($data);
                    if($result===false){
                        $historymodel->rollback();
                        $this->ajaxReturn(array('code' => 1,'msg'=>'发送失败'));
                    }
                }else{
                    //给下级进行极光推送
                    //1.查询所有下级角色允许进行推送的信息
                    $info=M('Saleman')->alias('a')->field('a.id,b.register_id')->where(array('a.role_id'=>$val,'a.is_allow'=>0))->join('JOIN __SALEMAN_PUSH__ b on a.id=b.saleman_id')->select();
                    //2.查询出所有下级角色不进行极光推送的信息
                    $saleman=M('Saleman')->field('id')->where(array('role_id'=>$val))->select();

                    //根据条件判断进行极光推送
                    $register_id=array();
                    $uid=array();
                    if($info){
                        //1.1允许进行极光推送
                        //1.1.1查询下级的register_id
                        foreach ($info as  $k=>$val) {
                            $register_id[$k]=$val['register_id'];
                            $uid[$k]['id']=$val['id'];
                        }

                        //1.1.2推送信息
                        sendNotify($register_id,$title,$txt);
                    }else{
                        //1.2不允许进行极光推送
                        foreach ($saleman as  $k=>$val) {
                            $uid[$k]['id']=$val['id'];
                        }
                    }
                    //1.3进行表存储，添加到信息表ymt_saleman_notice和ymt_push_history

                    //向数据库表中插入数据
                    $mydata = array(
                        'title' =>$title,
                        'content'=>$txt,
                        'type'=>$val,//代表所属的下级角色
                        'user_id'=>self::$user['id']
                    );
                    //向数据库表中插入数据
                    $result=M('PushHistory')->add( $mydata);
                    //添加业务员端的消息表
                    $mydata1=array();
                    foreach ($uid as  $k=>$val) {
                        $mydata1[$k]['title']=$title;
                        $mydata1[$k]['content']=$txt;
                        $mydata1[$k]['type']=1;//上级通知
                        $mydata1[$k]['send_user_id']=self::$user['id'];
                        $mydata1[$k]['send_name']=self::$user['real_name'];
                        $mydata1[$k]['accept_saleman_id']=$val['id'];
                        $mydata1[$k]['send_time']=time();
                    }
                    //向数据库表中插入数据
                    $result=M('SalemanNotice')->addALL($mydata1);
                    if($result===false){
                        $historymodel->rollback();
                        $this->ajaxReturn(array('code' => 1,'msg'=>'发送失败'));
                    }
                }

            }
            if($result===false){
                $historymodel->rollback();
                $this->ajaxReturn(array('code' => 1,'msg'=>'发送失败'));
            }else{
                $historymodel->commit();
                $this->ajaxReturn(array('code' => 0));
            }
        }else{
            $where['role_id']=$role_id;
            $data=M('Push')->field('role')->where(array('role_id'=>$role_id))->find();
            //查询当前用户的register_id
            $push_history=M('PushHistory')->where(array('user_id'=>self::$user['id']))->select();
            $push_info=decodeLabel($data['role']);
            $this->assign('push_info',$push_info);
            $this->assign('push',$push_history);
            $this->assign('role_id',$role_id);
            $this->display();
        }
    }

    /**
     * 删除数据(真删除)
     * @param $id
     */
    public function remove_push($id)
    {
        //删除信息
        $rst = M('PushHistory')->delete($id);
        if ($rst !== false) {
            $this->ajaxReturn(array('code' => 0));
        } else {
            $this->ajaxReturn(array('code' => 1, 'msg' => $rst->getError()));
        }
    }
    
    public function produce($originimg,$lastimg){
	    	$image = new \Think\Image();
	    	$path=__ROOT__.'/Uploads/Picture/salemanager';
	    	if (!file_exists($path)){
	    		mkdir($path);
	    	}
	    	$tmpImg = __ROOT__.$originimg;
	    	$image->open('.'.$tmpImg);
	    	// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
	    $re =$image->thumb(150, 150)->save($path.'/'.$lastimg);
    }

    /**
     * 获取大区经理助手列表
     * @param unknown $key
     * @param unknown $value
     */
    /**
     * 获取下级列表
     */
    public function getAssistant(){
    	$REQUEST = ( array ) I ( 'request.' );
    	if(IS_AJAX){ //下拉刷新
    		if(!empty($REQUEST['pageStart'])){
    				$userList = $this->saleModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'role_id'=>11,'status'=>C('SALEMANAGER_STATUS.NORMAL')),C('SALEMANAGER_PAGE_NUM'),$REQUEST['pageStart'],'id desc');
    		}else{
    			$userList=array();
    		}
    	}else{
    		$userList = $this->saleModel->findInfobyCondition(array('superior_id'=>self::$user['id'],'role_id'=>11,'status'=>C('SALEMANAGER_STATUS.NORMAL')),C('SALEMANAGER_PAGE_NUM'),0,'id desc');
    	}
    	$this->assign('userid',think_ucenter_encrypt($REQUEST['userid']));
    	foreach ($userList as $k=>$v){
    		$userList[$k]['id']=think_ucenter_encrypt($v['id']);
    	}
    	return $userList;
    }
	private function setParamters($key, $value) {
		$this->paramters [$key] = $value;
	}
	private function getParamters($key) {
		return $this->paramters [$key];
	}
}